import { editApi,fetchApiListById, delApiById, saveApi, fetchApiDetailById,testActionApi,apiUseConditionByQa,apiUseConditionByDialog} from '@/services/basiccapacity';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'customApi',
  state: {
    total: null,
    items: [],
    loading: false
  },

  effects: {
    *fetch({payload,callback}, { call, put }) {
      const response = yield call(fetchApiListById,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *delete({payload,callback}, { call, put }) {
      const response = yield call(delApiById,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'del',
        payload: response,
      });
    },
    *add({payload,callback}, { call, put }) {
      const response = yield call(saveApi,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *edit({payload,callback}, { call, put }) {
      const response = yield call(editApi,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *testApi({payload,callback}, { call, put }) {
      const response = yield call(testActionApi,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    * redirect ({ payload }, { put }) {
      yield put(routerRedux.push({ pathname: '/knowledgeDesign/customApi/edit', query: { ...payload }}));
    },
    * test ({ payload }, { put }) {
      yield put(routerRedux.push({ pathname: '/knowledgeDesign/customApi/testApi', query: { ...payload }}));
    },
    *fetchApiDetailById({payload,callback}, { call, put }) {
      const response = yield call(fetchApiDetailById,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getApiUseInfoByQa({payload,callback}, { call, put }) {
      const response = yield call(apiUseConditionByQa,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getApiUseInfoByDialog({payload,callback}, { call, put }) {
      const response = yield call(apiUseConditionByDialog,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    del(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  }
};
